﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using MyServer.TcpIP;
using MyServer.Common;

namespace ClientUI
{
    public partial class TcpIp : Form
    {
        public TcpIp()
        {
            InitializeComponent();
        }

        public static TcpIpCenter Center;
        TcpClient Client;

        private void button1_Click(object sender, EventArgs e)
        {
            IPAddress ipaddr = IPAddress.Parse(textBox1.Text);
            IPEndPoint serverEndp = new IPEndPoint(ipaddr, (int)numericUpDown1.Value);
            ipaddr = IPAddress.Parse(textBox2.Text);
            IPEndPoint localEndP = new IPEndPoint(ipaddr, (int)numericUpDown2.Value);
            Center = new TcpIpCenter(localEndP);
            Center.Start();
            Client = new TcpClient();
            Client.Connect(serverEndp);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.numericUpDown1.Maximum = IPEndPoint.MaxPort;
            this.numericUpDown2.Maximum = IPEndPoint.MaxPort;
            this.numericUpDown1.Minimum = IPEndPoint.MinPort;
            this.numericUpDown2.Minimum = IPEndPoint.MinPort;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Center.SendData(Client, Encoding.UTF8.GetBytes(textBox3.Text), "pkg");
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                this.textBox4.Text = (String)treeView1.SelectedNode.Tag;
            }
        }
    }
}
